package customer.web.beans;


import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.primefaces.event.SelectEvent;


import common.MessageCallback;
import customer.ejb.eaos.ExcursionManager;
import customer.ejb.eaos.HotelList;
import customer.ejb.eaos.MeanOfTransportationManager;
import customer.ejb.eaos.PersonalizedTPInterface;
import dtos.ExcursionDTO;
import dtos.ExcursionPersTPDTO;
import dtos.HotelDTO;
import dtos.MeanOfTransportationDTO;
import dtos.PersonalizedTPDTO;


@ManagedBean(name="personalizeBean")
@ViewScoped
public class PersonalizeBean {
	
 

	@EJB
	private PersonalizedTPInterface personalizedTPManager;
	private PersonalizedTPDTO personalizedTPDTO;
	private Date currentDate = new Date();
	
	private DataModel<HotelDTO> hotels;
	private HotelDTO selectedHotel;
	@EJB
	private HotelList hotelList;
	
	private DataModel<ExcursionDTO> excursions;
	private ExcursionDTO selectedExcursion;
	@EJB
	private ExcursionManager excursionManager;
	
	
	private DataModel<MeanOfTransportationDTO> toGos;
	private DataModel<MeanOfTransportationDTO> toReturns;
	private MeanOfTransportationDTO selectedToGo;
	@EJB
	private MeanOfTransportationManager transportationManager;
	
	private int nomberOfPeople;
	private List<ExcursionPersTPDTO> listJoin = new LinkedList<ExcursionPersTPDTO>();
	private DataModel<ExcursionDTO> excursionsInPersTP;
	private Date date;
	
	
	@PostConstruct
	public void init()
	{
		
		String idstring = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("predefinedTP_ID");
		String userid = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("user_ID");

		
		int id = Integer.parseInt(idstring);
		
			//FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "New Package", "You are currently creating a new package!"));
			personalizedTPDTO = personalizedTPManager.createPackage(id, userid);
			

			//get the hotel list
			String place = personalizedTPDTO.getHotelBean().getArrivalPlace();
			List<HotelDTO> h = new LinkedList<>();
			for(HotelDTO ho : hotelList.getHotelList(place)){
				h.add(ho);
			}
			hotels = new ListDataModel<>(h);
			
			//get the transportations to go list
			String departurePlace = personalizedTPDTO.getMeanOfTransportation1().getTrip().getDeparturePlace();
			String arrivalPlace = personalizedTPDTO.getMeanOfTransportation1().getTrip().getArrivalPlace();
			List<MeanOfTransportationDTO> transp1 = new LinkedList<>();
			for(MeanOfTransportationDTO tr : transportationManager.getTransportations(departurePlace, arrivalPlace)){
				transp1.add(tr);
			}
			toGos = new ListDataModel<>(transp1);
			
			
			//get the transportations to return list
			List<MeanOfTransportationDTO> transp2 = new LinkedList<>();
			for(MeanOfTransportationDTO tr : transportationManager.getTransportations(arrivalPlace, departurePlace)){
				transp2.add(tr);
			}
			toReturns = new ListDataModel<>(transp2);
			
			//get the excursion list
			excursionsInPersTP = new ListDataModel<>(excursionManager.getExcursionListPredefinedTP(id));
			refreshExcursionList();
			
			for (ExcursionDTO e : excursionsInPersTP){
				ExcursionPersTPDTO exc = new ExcursionPersTPDTO();
				exc.setExcursion(e);
				exc.setPersonalizedTp(personalizedTPDTO);
				listJoin.add(exc);
			}
			
	}
	
	
	public void refreshExcursionList(){
		String arrivalPlace = personalizedTPDTO.getMeanOfTransportation1().getTrip().getArrivalPlace();
		
		List<ExcursionDTO> exc = new LinkedList<>();
		for(ExcursionDTO e : excursionManager.getExcursionList(arrivalPlace)){
			exc.add(e);
		}
		

		for (ExcursionDTO e : excursionsInPersTP){
			for (ExcursionDTO e2 : exc){
				if (e2.getExcursionID() == e.getExcursionID()){
					exc.remove(e2);
				}
			}
		}
		excursions = new ListDataModel<>(exc);
	}
	
	public PersonalizedTPDTO getPersonalizedTPDTO(){
		return personalizedTPDTO;
	}
	
	public HotelDTO getSelectedHotel(){
		return selectedHotel;
	}
	public void setSelectedHotel (HotelDTO selectedHotel) {  
        this.selectedHotel = selectedHotel;  
    }
	
	public void handleStart(SelectEvent e)
	{
		if(e.getObject() instanceof Date)
		{
			getPersonalizedTPDTO().setDateOfDeparture((Date)e.getObject());
		}
	}
	
	public void handleEnd(SelectEvent e)
	{
		if(e.getObject() instanceof Date)
		{
			getPersonalizedTPDTO().setDateOfReturn((Date)e.getObject());
		}
	}
	
	public void handleSetDate( ExcursionDTO excursionDTO)
	{
		
			for (ExcursionPersTPDTO edto : listJoin){
				if (edto.getExcursion().getExcursionID() == excursionDTO.getExcursionID()){
					edto.setDate(date);
				}
				
			}

	}
	
	public Date getDate(){
		return date;
	}
	
	public void setDate(Date date){
		this.date=date;
	}
	
	
	
	public String savePersonalizedTP()
	{
		for (ExcursionPersTPDTO excurPers: listJoin){
			if (excurPers.getDate() == null){
				MessageCallback oc= new MessageCallback(FacesContext.getCurrentInstance());
				oc.onError("Set dates of excurions");
				return null;
			}
		}
		personalizedTPManager.savePersonalizedTP(personalizedTPDTO, new MessageCallback(FacesContext.getCurrentInstance()));
		for (ExcursionPersTPDTO e : listJoin){
			personalizedTPManager.saveExcursionPersTP(e);
		}
		return "myTravels?faces-redirect=true";
	}
	
	public Date getCurrentDate() {
	    return currentDate;
	}

	
	public DataModel<HotelDTO> getHotels(){
		return hotels;
	}
	
	public void changeHotel(HotelDTO hotel){
		personalizedTPDTO.setHotelBean(hotel);

	}
	

	
	
	public DataModel<ExcursionDTO> getExcursionsTravel(){
		List<ExcursionDTO> excursions = personalizedTPDTO.getExcursions();
		return new ListDataModel<>(excursions);
	}
	
	public ExcursionDTO getSelectedExcursion(){
		return selectedExcursion;
	}
	public void setSelectedExcursion (ExcursionDTO selectedExcursion) {  
        this.selectedExcursion = selectedExcursion;  
    } 
	
	
	public MeanOfTransportationDTO getSelectedToGo(){
		return this.selectedToGo;
	}
	
	public void setSelectedToGo(MeanOfTransportationDTO selectedToGo){
		this.selectedToGo = selectedToGo;
	}
	
	public DataModel<MeanOfTransportationDTO> getToGos(){
		return toGos;
	}
	
	public void changeToGo(MeanOfTransportationDTO t){
		personalizedTPDTO.setMeanOfTransportation1(t);

	}
	
	public void changeToReturn(MeanOfTransportationDTO t){
		personalizedTPDTO.setMeanOfTransportation2(t);

	}
	
	public DataModel<MeanOfTransportationDTO> getToReturns(){
		return toReturns;
	}
	
	
	public void removeExcursion(ExcursionDTO e){
		for (ExcursionPersTPDTO exc : listJoin){
			if (exc.getExcursion().getExcursionID() == e.getExcursionID()){
				listJoin.remove(exc);
			}
		}
		List<ExcursionDTO> excursionList = new LinkedList<ExcursionDTO>();
		for (ExcursionDTO edto : excursionsInPersTP){
			excursionList.add(edto);
		}
		
		for (ExcursionDTO exc : excursionList){
			if (exc.getExcursionID() == e.getExcursionID()){
				excursionList.remove(e);
			}
		}
		excursionsInPersTP = new ListDataModel<>(excursionList);

		refreshExcursionList();
		
	}
	
	public void addExcursion(ExcursionDTO e){

		ExcursionPersTPDTO exc = new ExcursionPersTPDTO();
		exc.setExcursion(e);
		exc.setPersonalizedTp(personalizedTPDTO);
		listJoin.add(exc);
		
		
		List<ExcursionDTO> excursionList = new LinkedList<ExcursionDTO>();
		for (ExcursionDTO edto : excursionsInPersTP){
			excursionList.add(edto);
		}
		
		excursionList.add(e);
		
		excursionsInPersTP = new ListDataModel<>(excursionList);
		
		refreshExcursionList();
	}
	
	public DataModel<ExcursionDTO> getExcursions(){
		return excursions;
	}
	
	public int getNomberOfPeople(){
		return this.nomberOfPeople;
	}
	
	public void setNomberOfPeople(int nomberOfPeople){
		this.nomberOfPeople = nomberOfPeople;
		this.personalizedTPDTO.setNomberOfPeople(nomberOfPeople);
	}

	public DataModel<ExcursionDTO> getExcursionsInPersTP(){
		return excursionsInPersTP;
	}
	
	

}
